<?php
namespace App\Models\SalesReport;

use App\Models\Basics\SysPlatAccount;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class PaymentDetailJP extends Model
{
    protected $table = 'sr_payment_detail_jp';

    protected $guarded = [];

    public $timestamps = false;

    public function account(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(SysPlatAccount::class,'account_id');
    }
    public function sku(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static $_type = [
        1 => "按去年同期汇率核算当期月报",
        2 => "按当期汇率核算去年同期月报"
    ];

    public static $_basicFields = [
        'report_date' => '日期',
        'report_type' => '报表类型',
        'account' => '账号',
        'seller' => '销售',
        'psku_code' => 'SKU',
        'psku_cn' => '产品名称',
        'season' => '季节性',
        'classify' => '品类',
        'seller_sku' => '平台SKU',
        'value_type' => '值类型'
    ];

    public static $_commonFields = [
        'rate_odds' => '倍率',
        'rate_odds_same_period' => '同期倍率',
        'sales_volume' => '销量',
        'sales_amount' => '销售额',
        'sales_amount_rmb' => '销售额RMB',
        'sales_amount_rmb_old' => '同期倍率销售额RMB',
        'selling_fees' => '佣金',
        'selling_fees_rmb' => '佣金RMB',
        'selling_fees_rmb_old' => '同期倍率佣金RMB',
        'fba_fees' => 'FBA交易费',
        'fba_fees_rmb' => 'FBA交易费RMB',
        'fba_fees_rmb_old' => '同期倍率FBA交易费RMB',
        'refund_num' => '退货数量',
        'refund_amount' => '退款金额',
        'refund_amount_rmb' => '退款金额RMB',
        'refund_amount_rmb_old' => '同期倍率退款金额RMB',
        'p_thfba' => '第三方FBA发货费',
        'p_thfba_rmb' => '第三方FBA发货费RMB',
        'p_thfba_rmb_old' => '同期倍率第三方FBA发货费RMB',
        'purchase_price_rmb' => '采购成本RMB',
        't_purchase_price_rmb' => '总采购成本RMB',
        'shipment_price_rmb' => '单位运费成本RMB',
        't_shipment_price_rmb' => '总运费成本RMB',
        'ms_fee' => 'Amazon月仓储费',
        'ms_fee_rmb' => 'Amazon月仓储费RMB',
        'ms_fee_rmb_old' => '同期倍率Amazon月仓储费RMB',
        'lts_fee' => 'Amazon长期仓储费',
        'lts_fee_rmb' => 'Amazon长期仓储费RMB',
        'lts_fee_rmb_old' => '同期倍率Amazon长期仓储费RMB',
        'p_icfee' => '库存补偿费',
        'p_icfee_rmb' => '库存补偿费RMB',
        'p_icfee_rmb_old' => '同期倍率库存补偿费RMB',
        'rm_fee' => '移除费用',
        'rm_fee_rmb' => '移除费用RMB',
        'rm_fee_rmb_old' => '同期倍率移除费用RMB',
        'rebates' => '促销费',
        'rebates_rmb' => '促销费RMB',
        'rebates_rmb_old' => '同期倍率促销费RMB',
        'spend' => '广告费用',
        'spend_rmb' => '广告费用RMB',
        'spend_rmb_old' => '同期倍率广告费用RMB',
        'reimburse_rmb' => '刷单费用RMB',
        'reimburse_os_rmb' => '运营服务费RMB',
        'reimburse_profit_rmb' => '刷单毛利扣减RMB',
        'ied_rmb' => '退货二次销售RMB',
        'rod_rmb' => '销毁货物损失RMB',
        'cplan_fee' => '清库计划费用',
        'cplan_fee_rmb' => '清库计划费用RMB',
        'cplan_fee_rmb_old' => '同期倍率清库计划费用RMB',
        'vat_fee' => 'VAT费用',
        'vat_fee_rmb' => 'VAT费用RMB',
        'vat_fee_rmb_old' => '同期倍率VAT费用RMB',
        'earning_rmb' => '销售收入',
        'earning_rmb_old' => '同期倍率销售收入',
        'total' => 'Total',
        'total_rmb' => 'TotalRMB',
        'total_rmb_old' => '同期倍率TotalRMB',
        'profit_rmb' => '毛利值RMB',
        'profit_rmb_old' => '同期倍率毛利值RMB',
    ];

    public static function getDownloadHeaders(): array
    {
        return self::$_basicFields + self::$_commonFields;
    }

    public static function getDownloadCommonData($row,$valueType): array
    {
        $temp = [];
        $temp['report_date'] = $row->report_date;
        $temp['report_type'] = self::$_type[$row->type];
        $temp['account'] = $row->account->account ?? '';
        $temp['seller'] = User::getUsernameById($row->seller_id);
        $temp['psku_code'] = $row->sku->psku_code ?? '';
        $temp['psku_cn'] = $row->sku->psku_cn ?? '';
        $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
        $temp['classify'] = $row->sku->classify->title ?? '';
        $temp['seller_sku'] = $row->seller_sku ?: '';
        $temp['value_type'] = $valueType;
        return $temp;
    }

    public static function getDownloadNumData($row,$commonFieldKeys): array
    {
        $temp = [];
        foreach ($commonFieldKeys as $key) {
            $temp[$key] = $row->{$key};
        }
        return $temp;
    }

    /**
     * 不需要统计百分比的字段
     * @var string[]
     */
    public static $_notPerFields = [
        'sales_volume',
        'sales_amount',
        'sales_amount_rmb',
        'sales_amount_rmb_old',
        'selling_fees',
        'fba_fees',
        'refund_amount',
        'p_thfba',
        'purchase_price_rmb',
        'shipment_price_rmb',
        'ms_fee',
        'lts_fee',
        'p_icfee',
        'rm_fee',
        'rebates',
        'spend',
        'cplan_fee',
        'vat_fee',
        'total',
    ];
    public static function getDownloadPerData($row,$commonFieldKeys): array
    {
        $temp = [];
        foreach ($commonFieldKeys as $key) {
            if(in_array($key,self::$_notPerFields)){
                $temp[$key] = '';
            }elseif(in_array($key,['rate_odds', 'rate_odds_same_period'])){
                $temp[$key] = $row->{$key};
            }elseif($key == 'refund_num'){
                $temp['refund_num'] = getPercentage($row->refund_num,$row->sales_volume);
            }else{
                $temp[$key] = getPercentage($row->{$key},$row->sales_amount_rmb);
            }
        }
        return $temp;
    }

    public static function getDownloadData($re): \Generator
    {
        $commonFieldKeys = array_keys(self::$_commonFields);
        foreach ($re as $row){
            $arr1 = self::getDownloadCommonData($row,'数值');
            $arr2 = self::getDownloadNumData($row,$commonFieldKeys);
            yield array_merge($arr1,$arr2);
            $arr3 = self::getDownloadCommonData($row,'占比');
            $arr4 = self::getDownloadPerData($row,$commonFieldKeys);
            yield array_merge($arr3,$arr4);
        }
    }
}
